
import pandas as pd
import numpy as np

# 使用列表创建
def fun1():
    data = [['Google', 10], ['Runoob', 12], ['Wiki', 17]]
    # 创建DataFrame
    df = pd.DataFrame(data, columns=['Site', 'Age'])
    # 使用astype方法设置每列的数据类型
    df['Site'] = df['Site'].astype(str)
    df['Age'] = df['Age'].astype(float)

    print(df)

# 使用字典创建
def fun2():
    data = {'Site':['Google', 'Runoob', 'Wiki'],
            'Age':[10, 12, 22]}
    df = pd.DataFrame(data)
    print(df)

# 使用ndarrays创建
def fun3():
    # 创建一个包含网站和年龄的二维ndarray
    ndarray_data = np.array([
        ['Google', 10],
        ['Runoob', 12],
        ['Wiki', 13]
    ])

    # 使用DataFrame构造函数创建数据帧
    df = pd.DataFrame(ndarray_data, columns=['Site', 'Age'])

    # 打印数据帧
    print(df)

# 使用字典创建
def fun4():
    data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}]
    df = pd.DataFrame(data)
    print(df)

def fun5():
    data = {
        'calories':[420, 380, 390],
        'duration':[50, 40, 45]
    }

    # 数据载入到DataFrame对象
    df = pd.DataFrame(data)

    print(df)
    # 返回第一行
    print(df.loc[0])
    # 返回第二行
    print(df.loc[1])

def fun6():
    data = {
        'calories':[420, 380, 390],
        'duration':[50, 40, 45]
    }
    df = pd.DataFrame(data)
    print(df.loc[[0, 1]])

def fun7():
    data = {
        'calories':[420, 380, 390],
        'duration':[50, 40, 45]
    }
    df = pd.DataFrame(data, index=["day1", "day2", "day3"])
    print(df)

def fun8():
    data = {
        'calories':[420, 380, 390],
        'duration':[50, 40, 45]
    }
    df = pd.DataFrame(data, index=["day1", "day2", "day3"])
    print(df.loc["day2"])

if __name__ == '__main__':
    fun8()